{extend name="base"/}
{block name="resources"}
<style>
    .design-sketch div{display: inline-block;border: 1px solid #ccc;padding: 10px;margin: 8px;border-radius: 2px;color: #555;white-space: nowrap;user-select: none;background-color: #fff;line-height: 1;}
    .design-sketch div i{position: absolute;top: -10px;right: -10px;display: none;width: 20px;height: 20px;border-radius: 10px;background-color: rgba(0, 0, 0, .5);color: #FFFFFF;text-align: center;line-height: 20px;z-index: 99;}
    .form-row{margin-top: 0;margin-left: 220px;}
    .express-sheet-rule .form-row{margin-left: 200px;}
</style>
{/block}
{block name="main"}
<div class="layui-form">
    <div class="layui-card card-common card-brief">
        <div class="layui-card-header">
            <span class="card-title">配送员信息</span>
        </div>
        <div class="layui-card-body">
            <div class="layui-form-item">
                <label class="layui-form-label"><span class="required">*</span>配送员名称：</label>
                <div class="layui-input-inline">
                    <input type="text" name="deliver_name" lay-verify="required|deliverName" class="layui-input len-long">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span class="required">*</span>配送员手机号：</label>
                <div class="layui-input-block">
                    <input type="text" name="deliver_mobile" lay-verify="required|deliverMobile" class="layui-input len-long">
                </div>
            </div>
        </div>
    </div>

    <div class="form-row">
        <button class="layui-btn" lay-submit lay-filter="save">保存</button>
        <button type="reset" class="layui-btn layui-btn-primary" onclick="back()">返回</button>
    </div>

</div>
{/block}
{block name="script"}
<script>
    layui.use(['form'], function() {
        var form = layui.form,
            repeat_flag = false; //防重复标识
        form.render();

        /**
         * 监听提交
         */
        form.on('submit(save)', function(data) {
            if (repeat_flag) return;
            repeat_flag = true;

            $.ajax({
                url: ns.url("shop/local/addDeliver"),
                data: data.field,
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    repeat_flag = false;
                    if (res.code == 0) {
                        layer.confirm('添加成功', {
                            title:'操作提示',
                            btn: ['返回列表', '继续添加'],
                            closeBtn: 0,
                            yes: function(){
                                location.href = ns.url("shop/local/deliverLists")
                            },
                            btn2: function () {
                                location.href = ns.url("shop/local/addDeliver")
                            }
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
        });

        /**
         * 表单验证
         */
        form.verify({
            deliverName: function(value){
                if (value == '') {
                    return  '配送员名称不能为空!';
                }
            },
            deliverMobile: function (value) {
                if (value == '') {
                    return '手机号不能为空!';
                }
                if (!isPoneAvailable(value)) {
                    return '请输入合法的手机号!'
                }
            }
        });
    });

    /**
     *  验证手机号正则
     */
    function isPoneAvailable(poneInput) {
        var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
        if (!myreg.test(poneInput)) {
            return false;
        } else {
            return true;
        }
    }

    function back(){
        location.href = ns.url("shop/local/deliverLists");
    }
</script>
{/block}